package model;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class Game {

	public static final int WIDTH = 800;
	public static final int HEIGHT = 500;
	
	public int scoreP;
	public int scoreAI;
	
	private Paddle paddle;
	private AI ai;
	private Ball ball;
	private Score score;
	private boolean isGameOver;
	
	public Game() {
		setUp();
	}
	
	public void update() {
		move();
		score.update();
		checkGameOver();
		checkCollision();
	}
	
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_KP_UP || e.getKeyCode() == KeyEvent.VK_UP)
			paddle.moveUp();
		else if (e.getKeyCode() == KeyEvent.VK_KP_DOWN || e.getKeyCode() == KeyEvent.VK_DOWN)
			paddle.moveDown();
		else if (e.getKeyCode() == KeyEvent.VK_R && isGameOver)
			setUp();
		else if (e.getKeyCode() == KeyEvent.VK_X)
			System.exit(0);
	}
	
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_KP_UP || e.getKeyCode() == KeyEvent.VK_UP)
			paddle.stop();
		else if (e.getKeyCode() == KeyEvent.VK_KP_DOWN || e.getKeyCode() == KeyEvent.VK_DOWN)
			paddle.stop();
	}
	
	public void draw(Graphics g) {
			ball.draw(g);
			paddle.draw(g);
			ai.draw(g);
	}
	
	private void move() {
		ball.move();
		paddle.move();
		ai.move();
	}
	
	public boolean isOver() {
		return isGameOver;
	}
	
	private void setUp() {
		scoreP = 0;
		scoreAI = 0;
		paddle = new Paddle();
		ball = new Ball();
		ai = new AI(ball);
		score = new Score(this);
		isGameOver = false;
	}
	
	public void checkGameOver() {
		if (ball.isOutOfBounds())
			isGameOver = true;
	}
	
	private void checkCollision() {
		ball.checkCollision(paddle, ai);
	}

	public int getScoreP() {
		return scoreP;
	}
	
	public int getScoreAI() {
		return scoreAI;
	}
	
}
